﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>UserRole</title>
    <link href="~/Content/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
    <div>
        <div id="test1" class="demo-transfer"></div>

    </div>
    <script src="~/Content/layui/layui.js"></script>
    <script>
        layui.use(['transfer', 'layer', 'util'], function () {
            var $ = layui.$
                , transfer = layui.transfer
                , layer = layui.layer
                , util = layui.util;
            $.get("/User/GetRole", {}, function (res) {
                var data1 = res;
                //基础效果
                transfer.render({
                    elem: '#test1'
                    , data: data1
                    , title: ['所有角色', '当前角色']
                    , value: [@ViewBag.RoleId]
                    , parseData: function (res) {
                        return {
                            "value": res.Id //数据值
                            , "title": res.RoleName //数据标题
                        }
                    }
                    , onchange: function (obj, index) {
                        if (index == 0) {//从左到右
                            action = "addRole";
                        } else {//从右到左
                            action = "delRole";
                        }
                        var ids = "";
                        //obj穿梭数据
                        for (var i = 0; i < obj.length; i++) {
                            ids += obj[i].value + ",";
                        }
                        ids = ids.substring(0, ids.length-1);
                        $.post("/User/SetRoleData", { ids: ids, action: action, userId:@ViewBag.UserId }, function () {
                            if (res.Success) {
                                layer.msg(res.Message);
                            } else {
                                layer.msg(res.Message);
                            }
                        })

                        @*var arr = ['左边', '右边'];
                        layer.alert('来自 <strong>' + arr[index] + '</strong> 的数据：' + JSON.stringify(obj)); //获得被穿梭时的数据*@
                    }
                });
            });
        });
    </script>
</body>
</html>
